package com.qire.manhua.controller;

import android.Manifest;
import android.graphics.Bitmap;
import android.view.View;

import com.bumptech.glide.request.target.Target;
import com.qire.common.GlideApp;
import com.qire.common.basic.StringUtil;
import com.qire.common.concurrent.ThreadUtil;
import com.qire.common.support.CustomLoadDialog;
import com.qire.common.support.base.EventController;
import com.qire.common.support.base.ProxyViewModel;
import com.qire.common.utils.FileManager;
import com.qire.common.utils.OSUtil;
import com.qire.common.utils.PermissionsChecker;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.manhua.BR;
import com.qire.manhua.activity.InviteFriendsActivity;
import com.qire.manhua.databinding.ActivityInviteFriendsBinding;
import com.qire.manhua.model.entity.ShareInfo;
import com.qire.manhua.model.viewModel.IInviteFriendsViewModel;
import com.qire.manhua.model.viewModel.impl.ShareItemViewModel;
import com.qire.other.share.ShareActionFactory;

import java.util.HashMap;
import java.util.concurrent.ExecutionException;

import androidx.annotation.NonNull;
import androidx.databinding.ViewDataBinding;

public class InviteFriendsController extends EventController<InviteFriendsActivity> implements AutoVariable, ShareGridController {

    private ProxyViewModel<IInviteFriendsViewModel> viewModel;

    public InviteFriendsController(@NonNull InviteFriendsActivity lifecycleOwner) {
        super(lifecycleOwner);
    }

    @Override
    protected void bindView() {
        ActivityInviteFriendsBinding viewDataBinder = owner.viewDataBinder();
        viewDataBinder.setInviteFriendsController(this);
    }

    @Override
    public void bindVariable(ViewDataBinding viewBind) {
        viewBind.setVariable(BR.shareGridController, this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(ProxyViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /*************************************************************************************************************************/

    public void backBtnOnClick(View view){
        owner.onBackPressed();
    }

    public void closeFaceToFaceBtnOnClick(View view){
        viewModel.set("faceToFaceVisible", View.GONE);
    }

    @Override
    public void shareItemOnClick(View view) {

        ShareInfo shareInfo = viewModel.<ShareInfo>getToValue("shareInfo");
        ShareItemViewModel shareItemViewModel = (ShareItemViewModel) view.getTag();

        switch (shareItemViewModel.shareType) {
            case WECHAT:
            case PLATFORM_QQ:
            case PLATFORM_QZONE:
                thirdPartyShare(shareInfo, shareItemViewModel);
                break;
            case SAVE_PIC:
                savePicShare(viewModel.<String>getToValue("postersImgUrl"));
                break;
            case FACE_TO_FACE:
                viewModel.set("faceToFaceVisible", View.VISIBLE);
                break;
            case COPY_LINKS:
                if(shareInfo == null) {
                    ToastHelper.showToast("网络问题：未获得分享数据");
                    return;
                }
                String text = shareInfo.urlPage;
                OSUtil.copyToClipboard(owner, "link", text);
                ToastHelper.showToast("链接复制成功");
                break;
        }

    }

    private void thirdPartyShare(ShareInfo shareInfo, ShareItemViewModel shareItemViewModel) {
        HashMap<String, Object> shareMap = new HashMap<>();

        shareMap.put("shareType", 2);//Platform.SHARE_IMAGE);
//        shareMap.put("ShareType", Platform.SHARE_WEBPAGE);
        shareMap.put("title", shareInfo.title);//测试分享的标题
        shareMap.put("titleUrl", shareInfo.urlPage);// 标题的超链接
        shareMap.put("text", shareInfo.content);//测试分享的文本
        shareMap.put("imageUrl", StringUtil.appendTimestampTo(shareInfo.urlImage));//测试分享的图片
        shareMap.put("site", shareInfo.title);//发布分享的网站名称
        shareMap.put("siteUrl", shareInfo.urlPage);//发布分享网站的地址

        ShareActionFactory.create(shareItemViewModel.shareType.shareName).doShareAction(shareMap);
    }

    private void savePicShare(final String postersImgUrl) {
        CustomLoadDialog.show(owner, "图片保存中\n请稍候...");

        PermissionsChecker permissionsChecker = new PermissionsChecker(owner, obtainWay -> {

            if (obtainWay == PermissionsChecker.ObtainWay.requestObtain) {
                ThreadUtil.<Void>once().andThen(v -> {
                    try {
                        Bitmap bitmap = GlideApp.with(owner).asBitmap().load(postersImgUrl)
                                .submit(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).get();
                        FileManager.saveToSystemGallery(bitmap, "QRInviteFriends.jpg");
                        ToastHelper.showToast("图片保存成功");
                    } catch (InterruptedException | ExecutionException e) {
                        e.printStackTrace();
                        ToastHelper.showToast("图片保存失败");
                    } finally {
                        CustomLoadDialog.dismissDialog();
                    }
                }).accept();
            } else {
                CustomLoadDialog.dismissDialog();
                ToastHelper.showToast("未获得授权，无法保存图片到相册");
            }

        });

        permissionsChecker.setPermissionTips("请允许奇热漫画获取您的存储访问权限，若禁止，将无法使用和上传图片信息。")
                .setPermissionDenyTip("请允许奇热漫画获取您的储访问权限，若禁止，将无法使用和上传图片信息。\n请在【设置-应用-奇热漫画-权限】中打开相关权限，以正常使用奇热漫画功能")
                .applyPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE});
    }

}
